package com.leetcode.algorithm.y22.m07;

import java.util.Comparator;
import java.util.PriorityQueue;

/**
 * 1046. 最后一块石头的重量
 * 
 * https://leetcode.cn/problems/last-stone-weight/
 * 
 * @author jie.deng
 *
 */
class Question1046Solution01 {
	
	public int lastStoneWeight(int[] stones) {
		PriorityQueue<Integer> queue = new PriorityQueue<>(Comparator.comparingInt(Integer::intValue).reversed());
		for (int i = 0; i < stones.length; i++) {
			queue.offer(stones[i]);
		}
		while (queue.size() >= 2) {
			int a1 = queue.poll().intValue();
			int a2 = queue.poll().intValue();
			if (a1 != a2) {
				queue.offer(Math.abs(a2 - a1));
			}
		}
		return queue.isEmpty() ? 0 : queue.poll().intValue();
	}
    
}